//
// Created by dddgzc on 2025/1/26.
//

#include "../include/SunflowerPlayer.h"

#include "PlayerSelectionScene.h"
#include "SunBullet.h"
#include "SunBulletEx.h"

SunflowerPlayer::SunflowerPlayer() {
    this->animationIdleRight.setAtlas(sunflowerIdleRightAtlas);
    this->animationIdleLeft.setAtlas(sunflowerIdleLeftAtlas);
    this->animationRunLeft.setAtlas(sunflowerRunLeftAtlas);
    this->animationRunRight.setAtlas(sunflowerRunRightAtlas);
    this->animationAttackExLeft.setAtlas(sunflowerAttackExLeftAtlas);
    this->animationAttackExRight.setAtlas(sunflowerAttackExRightAtlas);
    this->animationSunText.setAtlas(sunTextAtlas);
    this->animationDieRight.setAtlas(sunflowerDieRightAtlas);
    this->animationDieLeft.setAtlas(sunflowerDieLeftAtlas);

    this->animationIdleRight.setInterval(80);
    this->animationIdleLeft.setInterval(80);
    this->animationRunLeft.setInterval(80);
    this->animationRunRight.setInterval(80);
    this->animationAttackExLeft.setInterval(100);
    this->animationAttackExRight.setInterval(100);
    this->animationSunText.setInterval(100);
    this->animationDieRight.setInterval(150);
    this->animationDieLeft.setInterval(150);

    this->animationSunText.setIsLoop(false);
    this->animationAttackExLeft.setIsLoop(false);
    this->animationAttackExRight.setIsLoop(false);
    this->animationDieRight.setIsLoop(false);
    this->animationDieLeft.setIsLoop(false);

    animationSunText.setOnAnimationFinishedCallback([&]() {
    });

    animationAttackExLeft.setOnAnimationFinishedCallback([&]() {
        isAttackingEx = false;
        isSunTextVisible = false;
    });

    animationAttackExRight.setOnAnimationFinishedCallback([&]() {
        isAttackingEx = false;
        isSunTextVisible = false;
    });

    attackCd = 250;
}

void SunflowerPlayer::onAttack() {
    Bullet *bullet = new SunBullet();
    Vector2 bulletPosition;
    const Vector2 &bulletSize = bullet->getSize();
    bulletPosition.x = position.x + (width - bulletSize.x) / 2;
    bulletPosition.y = position.y + 10;

    bullet->setPosition(bulletPosition.x, bulletPosition.y);
    bullet->setVelocity(isFacingRight ? sunVelocity.x : -sunVelocity.x, sunVelocity.y);
    bullet->setCollideTarget(playerID == PlayerID::PLAYER_1 ? PlayerID::PLAYER_2 : PlayerID::PLAYER_1);
    bullet->setCallback([&]() { mp += 35; });
    bullets.push_back(bullet);
}

void SunflowerPlayer::onAttackEx() {
    Player::onAttackEx();
    isAttackingEx = true;
    isSunTextVisible = true;
    animationSunText.reset();
    isFacingRight ? animationAttackExRight.reset() : animationAttackExLeft.reset();
    //
    SunBulletEx* bulletEx = new SunBulletEx();
    Vector2 bulletPosition, bulletVelocity;

    Player* targetPlayer = playerID == PlayerID::PLAYER_2 ? player01 : player02;
    const Vector2 &bulletSize = bulletEx->getSize();
    const Vector2 &targetSize = targetPlayer->getSize();
    const Vector2 &targetPosition = targetPlayer->getPosition();
    bulletPosition.x = targetPosition.x + (targetSize.x - bulletSize.x) / 2;
    bulletPosition.y = - height;
    bulletVelocity.x = 0;
    bulletVelocity.y = sunExSpeed;

    bulletEx->setPosition(bulletPosition.x, bulletPosition.y);
    bulletEx->setVelocity(bulletVelocity.x, bulletVelocity.y);
    bulletEx->setCollideTarget(playerID == PlayerID::PLAYER_2 ? PlayerID::PLAYER_1 : PlayerID::PLAYER_2);
    bulletEx->setCallback([&]() {
        mp += 50;
    });
    bullets.push_back(bulletEx);
}

void SunflowerPlayer::onUpdate(int delta) {
    Player::onUpdate(delta);
    if (isSunTextVisible) {
        this->animationSunText.onUpdate(delta);
    }
}

void SunflowerPlayer::onDraw(const Camera &camera) {
    Player::onDraw(camera);
    if (isSunTextVisible) {
        Vector2 textPosition;
        textPosition.x = position.x - (width - 90) / 2;
        textPosition.y = position.y - 90;
        animationSunText.onDraw(camera, textPosition.x, textPosition.y);
    }
}


